package com.dragon.english_practice_back.entity.AIJson;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class TranslateQuestion implements Serializable {
    private String question;
    private String standardAnswer;

    // 静态的json转对象方法
    public static TranslateQuestion parse(String json) {
        TranslateQuestion translateQuestion = new TranslateQuestion();
        try {
            translateQuestion = JSONUtil.toBean(json, translateQuestion.getClass());
        } catch (Exception e) {
            log.error("json转对象失败", e);
        }
        return translateQuestion;
    }

    // 静态的对象转json方法
    public static String stringify(TranslateQuestion translateQuestion) {
        return JSONUtil.toJsonStr(translateQuestion);
    }

    // 静态的json转对象数组方法
    public static List<TranslateQuestion> parseArray(String json) {
        List<TranslateQuestion> translateQuestions = new ArrayList<>();
        try {
            translateQuestions = JSONUtil.toList(json, TranslateQuestion.class);
        }
        catch (Exception e) {
            log.error("json转对象数组失败", e);
        }
        return translateQuestions;
    }
}
